<template>
  <div class="card-container">
    <el-card class="box-card">
      <!-- 新增试题按钮 -->
      <div class="topLine">
        <p class="topTips">说明：目前支持学科和关键字条件筛选</p>
        <el-button @click="$router.push('/questions/new')" type="success" icon="el-icon-edit" size="small" style="float:right; margin-bottom:20px">新增试题
        </el-button>
      </div>
      <!-- 上部表单 -->
      <el-form ref="searchForm" :model="searchForm" label-width="80px" size="small">
        <!-- 第一行 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="学科" prop="subjectID">
              <el-select @change="changeSubject" v-model="searchForm.subjectID" placeholder="请选择" style="width: 100%;" filterable>
                <el-option v-for="item in subjectsList" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录" prop="catalogID">
              <el-select v-model="searchForm.catalogID" placeholder="请选择" style="width: 100%;" filterable>
                <el-option v-for="item in directoryList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签" prop="tags">
              <el-select v-model="searchForm.tags" placeholder="请选择" style="width: 100%;" filterable>
                <el-option v-for="item in tagList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" prop="keyword">
              <el-input v-model="searchForm.keyword" placeholder="根据题干搜索"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型" prop="questionType">
              <el-select v-model="searchForm.questionType" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in questionTypeList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" prop="difficulty">
              <el-select v-model="searchForm.difficulty" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in difficultyList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" prop="direction">
              <el-select v-model="searchForm.direction" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in directionList" :key="item" :label="item" :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" prop="creatorID">
              <el-select v-model="searchForm.creatorID" placeholder="请选择" style="width: 100%;">
                <el-option v-for="item in creatorList" :key="item.id" :label="item.username" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第三行 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注" prop="remarks">
              <el-input v-model="searchForm.remarks"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" prop="shortName">
              <el-input v-model="searchForm.shortName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-row>
              <el-col :span="12">
                <el-form-item label="城市" prop="province">
                  <el-select @change="cityChange" v-model="searchForm.province" placeholder="请选择" style="width: 100%;">
                    <el-option v-for="item in citySelect.provincesList" :key="item" :label="item" :value="item"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="" prop="city">
                  <el-select v-model="searchForm.city" placeholder="请选择" style="width: 100%;">
                    <el-option v-for="item in citySelect.citysList" :key="item" :label="item" :value="item"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-form-item style="text-align: right;">
              <el-button @click="resetForm">清除</el-button>
              <el-button type="primary" @click="searchForm.page = 1; getQuestionList()">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据总数条 -->
      <el-row>
        <el-col :span="24">
          <div class="grid-content">
            <i class="el-icon-info"></i> 共 {{ total }} 条记录
          </div>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%;">
        <el-table-column prop="number" label="试题编号" width="130%"></el-table-column>
        <el-table-column prop="subject" label="学科" width="100%"></el-table-column>
        <el-table-column prop="catalog" label="目录"></el-table-column>
        <el-table-column prop="questionType" label="题型">
          <template v-slot="scope">
            <span v-if="scope.row.questionType==='1'">单选</span>
            <span v-if="scope.row.questionType==='2'">多选</span>
            <span v-if="scope.row.questionType==='3'">简答</span>
          </template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="270%">
          <template v-slot="scope">
            <div v-html="scope.row.question"></div>
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="200%">
          <template v-slot="scope">
            <div>{{$moment(scope.row.addDate).format('YYYY-MM-DD HH:mm:ss')}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度">
          <template v-slot="scope">
            <span v-if="scope.row.difficulty==='1'">简单</span>
            <span v-if="scope.row.difficulty==='2'">一般</span>
            <span v-if="scope.row.difficulty==='3'">困难</span>
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="录入人"></el-table-column>
        <el-table-column label="操作" width="180%">
          <template v-slot="scope">
            <el-button @click="previewFn(scope.row.id)" type="primary" icon="el-icon-view" size="small" circle plain></el-button>
            <el-button @click="$router.push('/questions/new?id=' + scope.row.id)" type="success" icon="el-icon-edit" size="small" circle plain></el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger" icon="el-icon-delete" size="small" circle plain></el-button>
            <el-button @click="addStart(scope.row.id)" type="warning" icon="el-icon-check" size="small" circle plain></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background layout="prev, pager, next, sizes, jumper" :total="total" :page-sizes="[5, 10, 15, 20 , 25]" :page-size="Number(searchForm.pagesize)" :current-page="Number(searchForm.page)" @size-change="handleSizeChange" @current-change="handleCurrentChange" style="float:right;margin:20px 0">
      </el-pagination>

      <!-- 题目预览对话框 -->
      <QuestionsPreview :questionInfo='questionInfo' ref="questionp"></QuestionsPreview>
    </el-card>
  </div>
</template>

<script>
// 获取基础题录列表,基础题库删除,基础题库预览详情
import { list, remove, choiceAdd, detail } from '@/api/hmmm/questions.js'
// 获取学科目录简单列表
import { simple } from '@/api/hmmm/subjects.js'
// 获取二级目录简单列表
import { simple as simpleDirectoryAPI } from '@/api/hmmm/directorys.js'
// 标签简单列表
import { simple as simpleTagsAPI } from '@/api/hmmm/tags.js'
// 用户简单列表
import { simple as simpleUserAPI } from '@/api/base/users.js'
// 获取状态,难度,题型,方向
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js'
// 获取城市接口 市和县
import { provinces, citys } from '@/api/hmmm/citys.js'

// 导入子组件QuestionsPreview
import QuestionsPreview from '@/module-hmmm/components/questions-preview.vue'

export default {
  name: 'QuestionsBase',
  components: {
    // 导入自定义组件
    QuestionsPreview
  },
  data () {
    return {
      searchForm: {
        subjectID: null,
        difficulty: null,
        questionType: null,
        tags: null,
        province: null,
        city: null,
        keyword: null,
        remarks: null,
        shortName: null,
        direction: null,
        creatorID: null,
        catalogID: null,
        page: 1,
        pagesize: 5
      },
      // 子组件需要的数据
      questionInfo: {},
      // 关键字
      keyword: '',
      // 学科列表
      subjectsList: [],
      // 二级目录列表:
      directoryList: [],
      // 标签列表
      tagList: [],
      // 试题类型列表
      questionTypeList: questionType,
      // 试题难度列表
      difficultyList: difficulty,
      // 方向列表
      directionList: direction,
      // 城市列表
      citySelect: {
        provincesList: provinces(),
        citysList: []
      },
      // 录入人列表
      creatorList: [],
      // 表格数据
      tableData: [],
      // 总数
      total: 0
    }
  },
  methods: {
    // 重置表单
    resetForm (searchForm) {
      this.$refs.searchForm.resetFields()
    },
    // 预览
    async previewFn (id) {
      const res = await detail({ id: id })
      console.log(res)
      this.$refs.questionp.dialogVisible = true
      this.questionInfo = res.data
      this.$refs.questionp.videoVisible = false
    },
    // 加入精选
    addStart (id) {
      this.$confirm('确认加入精选题库吗?').then(async () => {
        await choiceAdd({
          id,
          choiceState: 1
        })
        this.$message.success('加入精选成功!')
        this.getQuestionList()
      })
        .catch((e) => {
          // 这个catch有两个作用 1.取消删除,2.有异常触发
          if (e === 'cancel') {
            console.log('取消操作')
          } else {
            this.$message.error('加入精选失败！')
          }
        })
    },
    // 删除
    handleDelete (id) {
      this.$confirm('确认要删除吗?').then(async () => {
        await remove({ id: id })
        this.$message.success('删除成功！')
        this.getQuestionList()
      })
        .catch((e) => {
          // 这个catch有两个作用 1.取消删除,2.有异常触发
          if (e === 'cancel') {
            console.log('取消删除')
          } else {
            this.$message.error('删除学科失败！')
          }
        })
    },
    // 更换省后,获取对应的市列表
    cityChange (val) {
      this.citySelect.citysList = citys(val)
    },
    // 更换学科,获取二级目录和标签列表
    async changeSubject (subjectID) {
      try {
        this.searchForm.catalogID = null
        this.searchForm.tags = null
        // 二级目录
        const ret1 = await simpleDirectoryAPI({ subjectID })
        this.directoryList = ret1.data
        // 标签
        const ret2 = await simpleTagsAPI({ subjectID })
        this.tagList = ret2.data
      } catch (e) {
        console.log(e)
        this.$message.error('获取列表失败')
      }
    },
    // 分页
    handleCurrentChange (page) {
      this.searchForm.page = page
      this.getQuestionList()
    },
    // 改变每页显示数量
    handleSizeChange (page) {
      this.searchForm.pagesize = page
      this.getQuestionList()
    },
    // 获取初始的学科列表,放入下拉框用,同时获取简单用户列表
    async getOptionsList () {
      try {
        const ret = await simple()
        // 学科列表
        this.subjectsList = ret.data
        // 获取简单用户
        const res2 = await simpleUserAPI()
        this.creatorList = res2.data
      } catch (e) {
        console.log(e)
        this.$message.error('获取简单列表失败')
      }
    },
    // 获取基础题库列表
    async getQuestionList () {
      try {
        const res = await list(this.searchForm)
        this.total = res.data.counts
        this.tableData = res.data.items
      } catch (e) {
        console.log(e)
        this.$message.error('获取题库列表失败')
      }
    }
  },
  created () {
    this.getQuestionList()
    this.getOptionsList()
  }
}
</script>

<style lang="less" scoped>
.topLine {
  height: 60px;
}
.topTips {
  float: left;
  font-size: 12px;
  margin-top: -3px;
  color: pink;
}
// 数据总数条
.grid-content {
  border-radius: 4px;
  min-height: 36px;
  line-height: 36px;
  background: #f4f4f5;
  color: #909399;
  font-size: 13px;
  padding-left: 16px;
  margin-bottom: 20px;
  .el-icon-info {
    font-size: 16px;
  }
}
</style>
